##################################################################################################################
# Code for Results in Appendix Sections A5.5, A5.6, and A5.7
##################################################################################################################
write.table(data.frame(Description=c("","","","Appendix Section A5.5: Robustness Check: Accounting for Additional Lobbying Firm Effects")),row.names=F,col.names=F,quote=F)

##################################################################################################################
# To run, please set the working directory to match the location of the folder containing all replication files.
##################################################################################################################
#setwd("Replication_PSRM_ST24")

#if not yet created, create "output" folder
if("output"%in%list.files()==F){dir.create("output")}

#Clear environment
rm(list=ls());gc();gc();gc();gc()

#If not yet installed, install package
if(length(find.package("dplyr",quiet=T))==0){install.packages("dplyr")}

#Load package
library(dplyr)

#Set POSIX locale
Sys.setlocale(locale="C")

#Load main data set
loball5ciA<-readRDS("Data/PSRM_main_dataset.rds")

#Appendix Section A5.5 
#Accounting for Lobbying Firm Effects by performing analysis within client-lobbying firm-lobbyist relationships 


###contract and in-house, half-yearly

#aggregate values for contract and for in-house lobbyists
agg67c2<-loball5ciA %>%
  filter(type%in%c("contract","inhouse")==T)%>%
  group_by(principal_cleaned,lobname_cleaned, lobbying_company_cleaned, halfyear,session,type) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg67d4<-agg67c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d4$sum_hours_cr0<-agg67d4$sum_hours
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]<-agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]/min(agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0])
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d4 <-agg67d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,lobname_cleaned,session,type) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg67d4$count<-1
agg67d4 <-agg67d4 %>% group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,session,type) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d4<-agg67d4[which(agg67d4$num_dyad_obs>1),]

###contract, half-yearly

#aggregate values for contract lobbyists
agg6c2<-loball5ciA %>%
  filter(type%in%c("contract")==T)%>%
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned, halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg6d4<-agg6c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d4$sum_hours_cr0<-agg6d4$sum_hours
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]<-agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]/min(agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0])
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d4 <-agg6d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
    )

agg6d4$count<-1
agg6d4 <-agg6d4 %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d4<-agg6d4[which(agg6d4$num_dyad_obs>1),]

###in-house, half-yearly

#aggregate values for in-house lobbyists
agg7c2<-loball5ciA %>%
  filter(type%in%c("inhouse")==T)%>%
  group_by(principal_cleaned,lobname_cleaned, lobbying_company_cleaned,halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg7d4<-agg7c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d4$sum_hours_cr0<-agg7d4$sum_hours
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]<-agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]/min(agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0])
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d4 <-agg7d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg7d4$count<-1
agg7d4 <-agg7d4 %>% group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d4<-agg7d4[which(agg7d4$num_dyad_obs>1),]


############ Aggregated to yearly

#contract and in-house, yearly
agg67d5<-agg67c2 %>%
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned, year=substr(halfyear,1,4),session,type) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d5$sum_hours_cr0<-agg67d5$sum_hours
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]<-agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]/min(agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0])
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d5 <-agg67d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,type) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg67d5$count<-1
agg67d5 <-agg67d5 %>% group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,type) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d5<-agg67d5[which(agg67d5$num_dyad_obs>1),]

###contract, yearly
agg6d5<-agg6c2 %>%
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d5$sum_hours_cr0<-agg6d5$sum_hours
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]<-agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]/min(agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0])
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d5 <-agg6d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg6d5$count<-1
agg6d5 <-agg6d5 %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d5<-agg6d5[which(agg6d5$num_dyad_obs>1),]

###in-house, yearly
agg7d5<-agg7c2 %>%
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d5$sum_hours_cr0<-agg7d5$sum_hours
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]<-agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]/min(agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0])
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d5 <-agg7d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg7d5$count<-1
agg7d5 <-agg7d5 %>% 
  group_by(principal_cleaned,lobname_cleaned,lobbying_company_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d5<-agg7d5[which(agg7d5$num_dyad_obs>1),]

write.table(data.frame(Description=c("","","","Results:")),row.names=F,col.names=F,quote=F)
write.table(data.frame(Description=c("","Figure A2: Within Client-Lobbying Firm-Lobbyist Deviations From Means in Lobbying Expen-
ditures and Hours Worked")),row.names=F,col.names=F,quote=F)

png("output/PSRM_figA2.png",width=12,height=8.83,units="in", res=300)
  par(mfrow=c(2,3))
  #Panel 1
  tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
  plot(main=c("(1) All Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
  #Panel 2
  tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
  plot(main=c("(2) Contract Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 3
  tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
  plot(main=c("(3) In-House Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 4
  tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
  plot(main=c("(4) All Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 5
  tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
  plot(main=c("(5) Contract Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 6
  tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
  plot(main=c("(6) In-House Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)

dev.off()

write.table(data.frame(Description=c("(N.B. The file for this figure is saved as 'PSRM_figA2.png' in the 'output' folder.)")),row.names=F,col.names=F,quote=F)

#Include code again so that Figure A2 also shown in R if code file is run using source()
par(mfrow=c(2,3))
#Panel 1
tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
plot(main=c("(1) All Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
#Panel 2
tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
plot(main=c("(2) Contract Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 3
tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
plot(main=c("(3) In-House Lobbyists"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 4
tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
plot(main=c("(4) All Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 5
tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
plot(main=c("(5) Contract Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 6
tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
plot(main=c("(6) In-House Lobbyists - Yearly Aggregation"),cex.main=1.5,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)


########
write.table(data.frame(Description=c("","","","Appendix Section A5.6: Robustness Check: Aggregation to Level of Lobbyist Employer")),row.names=F,col.names=F,quote=F)
# Results for Appendix Section A5.6
# Accounting for Lobbying Firm Effects

#N.B. If not yet set, need to un-comment below line and set working directory to folder containing "Data" folder with replication files
#setwd("Replication_PSRM_ST24")

#if not yet created, create "output" folder
if("output"%in%list.files()==F){dir.create("output")}

#Clear environment
rm(list=ls());gc();gc();gc();gc()

#If not yet installed, install package
if(length(find.package("dplyr",quiet=T))==0){install.packages("dplyr")}

#Load package
library(dplyr)

#Set POSIX locale
Sys.setlocale(locale="C")

#Load main data set
loball5ciA<-readRDS("Data/PSRM_main_dataset.rds")


write.table(data.frame(Description=c("","","","Appendix Section A5.6: Results:","")),row.names=F,col.names=F,quote=F)
write.table(data.frame(Description=c("There are 1,956 observations for non-licensed lobbyists that refer to clients' employees who are not 
licensed lobbyists ('non-lobbyists'), but still lobbied for the client (WIS. STAT. par 13.68(1)(cm)).")),row.names=F,col.names=F,quote=F)

print("length(which(loball5ciA$type=='nonlobbyist'))")
print(length(which(loball5ciA$type=="nonlobbyist")))
write.table(data.frame(Description=c("","The combined expenditures for these observations make up approximately 5% of overall expenditures, 
prior to being excluded for the main analysis.")),row.names=F,col.names=F,quote=F)
print("round(sum(loball5ciA$Rcomparable_total_amounts_cleaned_exp[loball5ciA$type=='nonlobbyist'])/sum(loball5ciA$Rcomparable_total_amounts_cleaned_exp),2))")
print(round(sum(loball5ciA$Rcomparable_total_amounts_cleaned_exp[loball5ciA$type=="nonlobbyist"])/
        sum(loball5ciA$Rcomparable_total_amounts_cleaned_exp),2))

###all lobbying employers, half-yearly
agg67c2<-loball5ciA %>%
  filter(typeIH%in%c("contract","inhouse")==T)%>%
  group_by(principal_cleaned, lobbying_company_cleaned, halfyear,session,typeIH) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg67d4<-agg67c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d4$sum_hours_cr0<-agg67d4$sum_hours
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]<-agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]/min(agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0])
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d4 <-agg67d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session,typeIH) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
    )

agg67d4$count<-1
agg67d4 <-agg67d4 %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session,typeIH) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d4<-agg67d4[which(agg67d4$num_dyad_obs>1),]


###lobbying firms, half-yearly
agg6c2<-loball5ciA %>%
  filter(typeIH%in%c("contract")==T)%>%
  group_by(principal_cleaned,lobbying_company_cleaned, halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg6d4<-agg6c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d4$sum_hours_cr0<-agg6d4$sum_hours
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]<-agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]/min(agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0])
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d4 <-agg6d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg6d4$count<-1
agg6d4 <-agg6d4 %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d4<-agg6d4[which(agg6d4$num_dyad_obs>1),]


###clients' employees, half-yearly
agg7c2<-loball5ciA %>%
  filter(typeIH%in%c("inhouse")==T)%>%
  group_by(principal_cleaned, lobbying_company_cleaned,halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

agg7d4<-agg7c2
#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d4$sum_hours_cr0<-agg7d4$sum_hours
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]<-agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]/min(agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0])
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d4 <-agg7d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg7d4$count<-1
agg7d4 <-agg7d4 %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d4<-agg7d4[which(agg7d4$num_dyad_obs>1),]


############ Aggregated to yearly

###all lobbying employers, yearly
agg67d5<-agg67c2 %>%
  group_by(principal_cleaned,lobbying_company_cleaned, year=substr(halfyear,1,4),session, typeIH) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d5$sum_hours_cr0<-agg67d5$sum_hours
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]<-agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]/min(agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0])
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d5 <-agg67d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,typeIH) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg67d5$count<-1
agg67d5 <-agg67d5 %>% 
  group_by(principal_cleaned,lobbying_company_cleaned,typeIH) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d5<-agg67d5[which(agg67d5$num_dyad_obs>1),]


###lobbying firms, yearly
agg6d5<-agg6c2 %>%
  group_by(principal_cleaned,lobbying_company_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d5$sum_hours_cr0<-agg6d5$sum_hours
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]<-agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]/min(agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0])
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d5 <-agg6d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg6d5$count<-1
agg6d5 <-agg6d5 %>% group_by(principal_cleaned,lobbying_company_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d5<-agg6d5[which(agg6d5$num_dyad_obs>1),]

###clients' employees, yearly
agg7d5<-agg7c2 %>%
  group_by(principal_cleaned,lobbying_company_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d5$sum_hours_cr0<-agg7d5$sum_hours
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]<-agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]/min(agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0])
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d5 <-agg7d5 %>%  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,lobbying_company_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg7d5$count<-1
agg7d5 <-agg7d5 %>% group_by(principal_cleaned,lobbying_company_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d5<-agg7d5[which(agg7d5$num_dyad_obs>1),]

write.table(data.frame(Description=c("","Results:")),row.names=F,col.names=F,quote=F)
write.table(data.frame(Description=c("","Figure A3: Within Client-Lobbying Employer-Lobbyist Deviations From Means in Lobbying 
Expenditures and Hours Worked")),row.names=F,col.names=F,quote=F)

png("output/PSRM_figA3.png",width=12,height=8.83,units = "in",res=300)
  
  par(mfrow=c(2,3))
  #Panel 1
  tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
  plot(main=c("(1) All Lobbying Employers"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
  #Panel 2
  tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
  plot(main=c("(2) Lobbying Firms"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 3
  tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
  plot(main=c("(3) Clients' Employees"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 4
  tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
  plot(main=c("(4) All Lobbying Employers - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 5
  tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
  plot(main=c("(5) Lobbying Firms - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 6
  tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
  plot(main=c("(6) Clients' Employees - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)

dev.off()

write.table(data.frame(Description=c("(N.B. The file for this figure is saved as 'PSRM_figA3.png' in the 'output' folder.)")),row.names=F,col.names=F,quote=F)

#include code again so that Figure A3 also shown in R if code file is run using source()
par(mfrow=c(2,3))
#Panel 1
tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
plot(main=c("(1) All Lobbying Employers"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
#Panel 2
tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
plot(main=c("(2) Lobbying Firms"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 3
tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
plot(main=c("(3) Clients' Employees"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 4
tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
plot(main=c("(4) All Lobbying Employers - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 5
tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
plot(main=c("(5) Lobbying Firms - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 6
tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
plot(main=c("(6) Clients' Employees - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)



########
# Results for Appendix Section A5.7
# Robustness Check: Aggregation to the Client Level
write.table(data.frame(Description=c("","","","Appendix Section A5.7: Robustness Check: Aggregation to the Client Level")),row.names=F,col.names=F,quote=F)

#N.B. If not yet set, need to un-comment below line and set working directory to folder containing "Data" folder with replication files
#setwd("Replication_PSRM_ST24")

#if not yet created, create "output" folder
if("output"%in%list.files()==F){dir.create("output")}

#Clear environment
rm(list=ls());gc();gc();gc();gc()

#If not yet installed, install package
if(length(find.package("dplyr",quiet=T))==0){install.packages("dplyr")}

#Load package
library(dplyr)

#Set POSIX locale
Sys.setlocale(locale="C")

#Load main data set
loball5ciA<-readRDS("Data/PSRM_main_dataset.rds")

# Aggregating to principal level 

agg67c2<-loball5ciA %>%
  filter(typeIH%in%c("contract","inhouse")==T)%>%
  group_by(principal_cleaned, halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d4<-agg67c2
agg67d4$sum_hours_cr0<-agg67d4$sum_hours
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]<-agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0]/min(agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0>0])
agg67d4$sum_hours_cr0[agg67d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d4 <-agg67d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )


agg67d4$count<-1
agg67d4 <-agg67d4 %>% group_by(principal_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d4<-agg67d4[which(agg67d4$num_dyad_obs>1),]


agg6c2<-loball5ciA %>%
  filter(typeIH%in%c("contract")==T)%>%
  group_by(principal_cleaned, halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d4<-agg6c2
agg6d4$sum_hours_cr0<-agg6d4$sum_hours
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]<-agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0]/min(agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0>0])
agg6d4$sum_hours_cr0[agg6d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d4 <-agg6d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg6d4$count<-1
agg6d4 <-agg6d4 %>% group_by(principal_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d4<-agg6d4[which(agg6d4$num_dyad_obs>1),]


agg7c2<-loball5ciA %>%
  filter(typeIH%in%c("inhouse")==T)%>%
  group_by(principal_cleaned,halfyear,session) %>% 
  summarise(.groups="keep",sum_hours=sum(hours,na.rm=T),sum_amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d4<-agg7c2
agg7d4$sum_hours_cr0<-agg7d4$sum_hours
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]<-agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0]/min(agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0>0])
agg7d4$sum_hours_cr0[agg7d4$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d4 <-agg7d4 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned,session) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg7d4$count<-1
agg7d4 <-agg7d4 %>% 
  group_by(principal_cleaned,session) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d4<-agg7d4[which(agg7d4$num_dyad_obs>1),]


############ Aggregated to yearly

agg67d5<-agg67c2 %>%
  group_by(principal_cleaned, year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg67d5$sum_hours_cr0<-agg67d5$sum_hours
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]<-agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0]/min(agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0>0])
agg67d5$sum_hours_cr0[agg67d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg67d5 <-agg67d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg67d5$count<-1

agg67d5 <-agg67d5 %>% group_by(principal_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg67d5<-agg67d5[which(agg67d5$num_dyad_obs>1),]


agg6d5<-agg6c2 %>%
  group_by(principal_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg6d5$sum_hours_cr0<-agg6d5$sum_hours
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]<-agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0]/min(agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0>0])
agg6d5$sum_hours_cr0[agg6d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg6d5 <-agg6d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T))
  )

agg6d5$count<-1
agg6d5 <-agg6d5 %>% group_by(principal_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg6d5<-agg6d5[which(agg6d5$num_dyad_obs>1),]

#
agg7d5<-agg7c2 %>%
  group_by(principal_cleaned,year=substr(halfyear,1,4),session) %>%
  summarise(.groups="keep",sum_hours=sum(sum_hours,na.rm=T),sum_amount=sum(sum_amount,na.rm=T))

#create hours worked variable so that later log transformation puts zero weight on extensive margin
agg7d5$sum_hours_cr0<-agg7d5$sum_hours
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]<-agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0]/min(agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0>0])
agg7d5$sum_hours_cr0[agg7d5$sum_hours_cr0==0]<-1

#create variables for analyses
agg7d5 <-agg7d5 %>%  
  filter(sum_amount>0) %>% 
  group_by(principal_cleaned) %>%
  mutate(
    dif_log_mean_amount=log(sum_amount)-log(mean(sum_amount,na.rm=T)),
    dif_log_mean_hours_cr0=log(sum_hours_cr0)-log(mean(sum_hours_cr0,na.rm=T)),
  )

agg7d5$count<-1
agg7d5 <-agg7d5 %>% group_by(principal_cleaned) %>%
  mutate(num_dyad_obs=sum(count)) 
#exclude observations when num_dyad_obs==1
agg7d5<-agg7d5[which(agg7d5$num_dyad_obs>1),]


write.table(data.frame(Description=c("","Results:")),row.names=F,col.names=F,quote=F)
write.table(data.frame(Description=c("","Figure A4: Within Client Deviations From Means in Lobbying Expenditures and Hours Worked")),row.names=F,col.names=F,quote=F)

png("output/PSRM_figA4.png",width=12,height=8.83,units="in",res=300)

  par(mfrow=c(2,3))
  #Panel 1
  tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
  plot(main=c("(1) All Lobbyists"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab=
         "Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
  #Panel 2
  tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
  plot(main=c("(2) Contract Lobbying"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 3
  tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
  plot(main=c("(3) In-House Lobbying"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 4
  tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
  plot(main=c("(4) All Lobbyists - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",0", 2+tt$parameter%%1000,sep=""),cex=1.5) #add zero so that value n gets shown as 9,028 and not 9,28
  lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 5
  tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
  plot(main=c("(5) Contract Lobbying - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
  #Panel 6
  tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
  plot(main=c("(6) In-House Lobbying - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
       xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
  text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
  legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
  lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)

dev.off()

write.table(data.frame(Description=c("(N.B. The file for this figure is saved as 'PSRM_figA4.png' in the 'output' folder.)")),row.names=F,col.names=F,quote=F)

#Include code again so that Figure A4 also shown in R if code file is run using source()
par(mfrow=c(2,3))
#Panel 1
tt<-cor.test(agg67d4$dif_log_mean_amount,agg67d4$dif_log_mean_hours_cr0)
plot(main=c("(1) All Lobbyists"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5, x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab=
       "Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg67d4$dif_log_mean_amount,y=agg67d4$dif_log_mean_hours_cr0,f=0.68),lty=2)
#Panel 2
tt<-cor.test(agg6d4$dif_log_mean_amount,agg6d4$dif_log_mean_hours_cr0)
plot(main=c("(2) Contract Lobbying"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d4$dif_log_mean_amount,y=agg6d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 3
tt<-cor.test(agg7d4$dif_log_mean_amount,agg7d4$dif_log_mean_hours_cr0)
plot(main=c("(3) In-House Lobbying"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Biennium Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Biennium Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d4$dif_log_mean_amount,y=agg7d4$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 4
tt<-cor.test(agg67d5$dif_log_mean_amount,agg67d5$dif_log_mean_hours_cr0)
plot(main=c("(4) All Lobbyists - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",0", 2+tt$parameter%%1000,sep=""),cex=1.5) #add zero so that value n gets shown as 9,028 and not 9,28
lines(lowess(x=agg67d5$dif_log_mean_amount,y=agg67d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 5
tt<-cor.test(agg6d5$dif_log_mean_amount,agg6d5$dif_log_mean_hours_cr0)
plot(main=c("(5) Contract Lobbying - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg6d5$dif_log_mean_amount,y=agg6d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)
#Panel 6
tt<-cor.test(agg7d5$dif_log_mean_amount,agg7d5$dif_log_mean_hours_cr0)
plot(main=c("(6) In-House Lobbying - Yearly Aggregation"),cex.main=1.45,cex.lab=1.2,cex.axis=1.5,x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,
     xlim=c(-8,2),ylim=c(-8,2),cex=1,col="gray",xlab="Ln(Dyad Amount)-Ln(Dyad Mean Amount)",ylab="Ln(Dyad Hours)-Ln(Dyad Mean Hours)")
text(x=-5.75,y=1.85,paste("r = ",round(tt$estimate,3)," (",round(sqrt( (1-tt$estimate^2)/(tt$parameter)),3),")",sep=""),cex=1.5)
legend(bg="white",bty = "n","bottomright",paste("n = ",floor(tt$parameter/1000),",", 2+tt$parameter%%1000,sep=""),cex=1.5)
lines(lowess(x=agg7d5$dif_log_mean_amount,y=agg7d5$dif_log_mean_hours_cr0,f=0.68) ,lty=2)


